﻿//========================================================= 
/**@file WinComIC_DevUpdater.h
 * @brief Windows下串口升级工具
 * 
 * @date 2017-01-21   11:03:09
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_WINCOMIC_DEVUPDATER_H_
#define _LIBZHOUYB_WINCOMIC_DEVUPDATER_H_
//--------------------------------------------------------- 
#include "COM_IC_DevUpdater.h"

#include "../../test_frame/linker/TestLinkerHelper.h"
using zhou_yb::application::test::TestLinkerHelper;

#include "../../tools/ParamHelper.h"
using zhou_yb::application::tools::ParamHelper;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace updater {
//--------------------------------------------------------- 
/// Windows下串口设备检测升级状态连接器
template<class TComDevice>
struct WinComUpdateModeTestLinker : public TestLinker<TComDevice>
{
    //----------------------------------------------------- 
    /**
     * @brief 扫描串口,并发送升级切换指令
     * 
     * @param [in] dev 需要操作的设备
     * @param [in] arg 参数列表
     * - 参数:
     *  - Name : string 串口设备名称
     * .
     * @param [in] printer 文本输出器
     */
    virtual bool Link(TComDevice& dev, IStringArguments& arg, TextPrinter& printer)
    {
        // 如果没有配置则自动去枚举串口
        uint baud = CBR_9600;
        uint upgradeBaud = CBR_115200;

        byte gate = 0;
        string devName = arg["Name"].To<string>("AUTO");
        uint port = ParamHelper::ParseCOM(devName.c_str(), gate, baud);

        // 没有检测到串口
        if(port < 1)
        {
            printer.TextPrint(TextPrinter::TextLogger, "检测串口失败");
            return false;
        }

        TestLinkerHelper::LinkTimeoutBehavior(dev, arg, printer);
        // 轮询每个端口判断连接状态
        if(ComDeviceHelper::OpenDevice<TComDevice>(dev, port, upgradeBaud) == DevHelper::EnumSUCCESS)
        {
            if(ComUpdateModeSwitcher::IsUpgradeMode(dev))
                return true;
            dev.Close();
        }
        /* 设备处于正常模式 */
        if(ComDeviceHelper::OpenDevice<TComDevice>(dev, port, baud) != DevHelper::EnumSUCCESS)
        {
            printer.TextPrint(TextPrinter::TextLogger, "正常模式连接设备失败");
            return false;
        }
        if(!TestLinkerHelper::LinkCommand(dev, arg, printer))
        {
            printer.TextPrint(TextPrinter::TextLogger, "发送控制指令失败");
            dev.Close();
            return false;
        }
        ComUpdateModeSwitcher::ToUpgradeMode(dev);
        dev.Close();
        return false;
    }
    /// 关闭串口
    virtual bool UnLink(TComDevice& dev, TextPrinter&)
    {
        dev.Close();
        return true;
    }
    //----------------------------------------------------- 
};
//--------------------------------------------------------- 
} // namespace updater
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_WINCOMIC_DEVUPDATER_H_
//========================================================= 